package main

import (
	"context"
	"fmt"
	"log"
	"net/http"

	"example.com/calculator/calculator/v1"
	"example.com/calculator/calculator/v1/calculatorv1connect"
	// 补充 connect-go 包的导入，并指定别名 connect
	connect "github.com/bufbuild/connect-go"
)

func main() {
	// 创建客户端
	client := calculatorv1connect.NewCalculatorServiceClient(
		http.DefaultClient,
		"http://localhost:8080",
	)
	
	// 测试加法
	addReq := &calculatorv1.AddRequest{A: 25, B: 30}
	addResp, err := client.Add(context.Background(), connect.NewRequest(addReq))
	if err != nil {
		log.Fatalf("Add request failed: %v", err)
	}
	fmt.Printf("25 + 30 = %v\n", addResp.Msg.Result)
	
	// 测试减法
	subReq := &calculatorv1.SubtractRequest{A: 100, B: 45}
	subResp, err := client.Subtract(context.Background(), connect.NewRequest(subReq))
	if err != nil {
		log.Fatalf("Subtract request failed: %v", err)
	}
	fmt.Printf("100 - 45 = %v\n", subResp.Msg.Result)
	
	// 测试乘法
	mulReq := &calculatorv1.MultiplyRequest{A: 15, B: 6}
	mulResp, err := client.Multiply(context.Background(), connect.NewRequest(mulReq))
	if err != nil {
		log.Fatalf("Multiply request failed: %v", err)
	}
	fmt.Printf("15 * 6 = %v\n", mulResp.Msg.Result)
	
	// 测试除法（正常情况）
	divReq := &calculatorv1.DivideRequest{A: 100, B: 4}
	divResp, err := client.Divide(context.Background(), connect.NewRequest(divReq))
	if err != nil {
		log.Fatalf("Divide request failed: %v", err)
	}
	fmt.Printf("100 / 4 = %v\n", divResp.Msg.Result)
	
	// 测试除法（除零情况）
	divZeroReq := &calculatorv1.DivideRequest{A: 50, B: 0}
	divZeroResp, err := client.Divide(context.Background(), connect.NewRequest(divZeroReq))
	if err != nil {
		// 除零错误时直接打印错误信息，不访问响应（此时响应可能为 nil）
		fmt.Printf("50 / 0 = (Error: %v)\n", err)
		return
	}
	fmt.Printf("50 / 0 = %v\n", divZeroResp.Msg.Result)
}